import getCurrentColor from "~background/actions/getCurrentColor";
import changeColor from "~background/actions/changeColor";
import highlightText from "~background/actions/highlightText";
import {wrapResponse} from "~background/utils";
import getHighlights from "~background/actions/getHighlights";
import updateComments from "~background/actions/updateComments";
import highlightImage from "~background/actions/highlightImage";
import highlightPage from "~background/actions/highlightPage";
import getColorOptions from "~background/actions/getColorOptions";
import showHighlight from "~background/actions/showHighlight";
import getLostHighlights from "~background/actions/getLostHighlights";


const initialize = () => {
  console.log("initialized...");
  initializeContextMenus();
  initializeContextMenuEventListeners();
  initializeMessageEventListeners();
}

const initializeContextMenus = () => {
  // Add option when right-clicking
  chrome.runtime.onInstalled.addListener(async () => {
    console.log("onInstalled...");
    // remove existing menu items
    chrome.contextMenus.removeAll();

    chrome.contextMenus.create({ title: 'Highlight Image', id: 'highlight-image', contexts: ['image'] });
    chrome.contextMenus.create({ title: 'Highlight', id: 'highlight', contexts: ['selection'] });
    chrome.contextMenus.create({ title: 'Toggle Cursor', id: 'toggle-cursor' });
    chrome.contextMenus.create({ title: 'Highlighter color', id: 'highlight-colors' });
    chrome.contextMenus.create({ title: 'Yellow', id: 'yellow', parentId: 'highlight-colors', type: 'radio' });
    chrome.contextMenus.create({ title: 'Blue', id: 'blue', parentId: 'highlight-colors', type: 'radio' });
    chrome.contextMenus.create({ title: 'Green', id: 'green', parentId: 'highlight-colors', type: 'radio' });
    chrome.contextMenus.create({ title: 'Pink', id: 'pink', parentId: 'highlight-colors', type: 'radio' });
    chrome.contextMenus.create({ title: "Dark", id: "dark", parentId: "highlight-colors", type: "radio" });

    // Get the initial selected color value
    const { title: colorTitle } = await getCurrentColor();
    chrome.contextMenus.update(colorTitle, { checked: true });
  });
}

const initializeContextMenuEventListeners = () => {
  chrome.contextMenus.onClicked.addListener(({ menuItemId, parentMenuItemId }) => {
    if (parentMenuItemId === 'highlight-color') {
      changeColor(menuItemId);
      return;
    }

    switch (menuItemId) {
      case 'highlight':
        highlightText();
        break;
      case 'highlight-image':
        highlightImage();
        break;
      // case 'toggle-cursor':
      //   toggleHighlighterCursor();
      //   break;
    }
  });
}

const initializeMessageEventListeners = () => {
  // Listen to messages from content scripts
  /* eslint-disable consistent-return */
  chrome.runtime.onMessage.addListener((request, _sender, sendResponse) => {
    if (!request.action) return;

    switch (request.action) {
      case 'get-highlights':
        wrapResponse(getHighlights(), sendResponse);
        return true; // return asynchronously
      case 'get-lost-highlights':
        wrapResponse(getLostHighlights(), sendResponse);
        return true; // return asynchronously
      case 'show-highlight':
        return showHighlight(request.highlightId);
      case 'get-current-color':
        wrapResponse(getCurrentColor(), sendResponse);
        return true; // return asynchronously
      case 'get-color-options':
        wrapResponse(getColorOptions(), sendResponse);
        return true; // return asynchronously
      case 'update-comments':
        updateComments(request.highlightId, request.viewstate, request.comments);
        return;
      case 'create-page-comments':
        wrapResponse(highlightPage(request.color, request.comments), sendResponse);
        return true; // return asynchronously
    }
  });
  /* eslint-enable consistent-return */
}

initialize();
